{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "a1be3085",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/data_connectors/MongoDemo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "effeb5a7-8544-4ee4-8c11-bad0d8165394",
   "metadata": {},
   "source": [
    "# MongoDB Reader\n",
    "Demonstrates our MongoDB data connector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9df1ec0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index-readers-mongodb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60355655",
   "metadata": {},
   "outputs": [],
   "source": [
    "import logging\n",
    "import sys\n",
    "\n",
    "logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n",
    "logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c1f8b93d",
   "metadata": {},
   "source": [
    "If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙 and pymongo."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d7e889da",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install llama-index pymongo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6ea1f66d-10ed-4417-bdcb-f8a894836ea5",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core import SummaryIndex\n",
    "from llama_index.readers.mongodb import SimpleMongoReader\n",
    "from IPython.display import Markdown, display\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da90589a-fb44-4ec6-9706-753dba4fa968",
   "metadata": {},
   "outputs": [],
   "source": [
    "host = \"<host>\"\n",
    "port = \"<port>\"\n",
    "db_name = \"<db_name>\"\n",
    "collection_name = \"<collection_name>\"\n",
    "# query_dict is passed into db.collection.find()\n",
    "query_dict = {}\n",
    "field_names = [\"text\"]\n",
    "reader = SimpleMongoReader(host, port)\n",
    "documents = reader.load_data(\n",
    "    db_name, collection_name, field_names, query_dict=query_dict\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "341295df-2029-4728-ab3d-2ee178a7e6f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "index = SummaryIndex.from_documents(documents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01c26b9d-49ec-4a6e-9c61-5c06bb86bbb2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# set Logging to DEBUG for more detailed outputs\n",
    "query_engine = index.as_query_engine()\n",
    "response = query_engine.query(\"<query_text>\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f160c678-2fb5-4d6d-b2bc-87abb61cfdec",
   "metadata": {},
   "outputs": [],
   "source": [
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
